package com.chixing.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 封装了获得数据库连接对象和关闭数据数据库
 * 从安全性，耦合性 需要到哪都封装数据的连接与关闭操作
 *
 */
public class DBConnection {
    private static Connection connection= null;
    public static Connection getConnection(){
        try {
            Class.forName(DataSourceFileUtil.getDriverName());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            connection=DriverManager.getConnection(DataSourceFileUtil.getUrl(), DataSourceFileUtil.getUserName(), DataSourceFileUtil.getPassword());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }
    public static void close(Connection conn){
        if(conn != null){
            try{
                conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
